package servlet.hou;

import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;



//修改卫生信息
@WebServlet("/hou/updateHygByHou")
public class UpdateHygByHouServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer hygId = Integer.valueOf(request.getParameter("hygId"));
        Integer dorNumber = null;
        if (request.getParameter("dorNumber") != null) {
            dorNumber = Integer.valueOf(request.getParameter("dorNumber"));

        }

        String hygDes = request.getParameter("hygDes");
        Integer checkSore = null;
        if (request.getParameter("checkSore") != null) {
            checkSore = Integer.valueOf(request.getParameter("checkSore"));

        }

        String user_token = request.getHeader("user_token_header");

        Integer doId = JwtUtils.getHouIdFromToken(user_token,"houId");
        String check_person = JwtUtils.getHouNameFromToken(user_token,"houName");
        Integer data = null;
        try {
            data = UserDaoImpl.updateHygByHou(hygId, hygDes, dorNumber, checkSore,doId,check_person);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        Result result = new Result();
        if (data != 0) {
            result.success(data);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        } else {
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }

    }
}
